package com.tianya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianya.common.Results;
import com.tianya.domain.dto.StudentRequestDTO;
import com.tianya.domain.entity.Student;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.StudentVO;
import com.tianya.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TianYaGuoKe
 * @date 2025/5/24 20:28
 * @description StudentController
 */
@Api("学生管理")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;


    @ApiOperation("根据班级id获取学生列表")
    @GetMapping("/list/{id}")
    public Results<List<StudentVO>> list(@PathVariable Long id) {
        return Results.success(studentService.listStudent(id));
    }

    @ApiOperation("根据班主任id获取班级学生")
    @GetMapping("/list/headerTeacher/{id}")
    public Results<List<StudentVO>> listStudentByHeaderTeacher(@PathVariable Long id) {
        return Results.success(studentService.listStudentByHeaderTeacher(id));
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Results<PageResultVO<StudentVO>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, StudentRequestDTO studentRequestDTO) {
        return Results.success(studentService.listPage(pageNum, pageSize, studentRequestDTO));
    }
    @ApiOperation("创建学生")
    @PostMapping
    public Results createStudent(@RequestBody StudentRequestDTO studentRequestDTO) {
        studentService.createStudent(studentRequestDTO);
        return Results.success();
    }

    @ApiOperation("编辑学生")
    @PutMapping("/{id}")
    public Results editStudent(@PathVariable Long id,@RequestBody Student student) {
        studentService.editStudent(id,student);
        return Results.success();
    }

    @ApiOperation("删除学生")
    @DeleteMapping("/{id}")
    public Results deleteStudent(@PathVariable Long id) {
        studentService.removeById(id);
        return Results.success();
    }

    @ApiOperation("查看详情")
    @GetMapping("/{id}")
    public Results<Student> getStudent(@PathVariable Long id) {
        Student student = studentService.getById(id);
        return Results.success(student);
    }

    @ApiOperation("家长查看学生")
    @GetMapping("/parent/{parent_id}")
    public Results<Student> getStudentByParentId(@PathVariable Long parent_id) {
        Student student = studentService.getOne(new QueryWrapper<Student>()
                .eq("parent_id", parent_id));
        return Results.success(student);
    }
}
